using UnityEngine;
using QFramework;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class KnifeBullet : BulletFathers
	{

        public Transform startPos;
        public override Transform StartPos => startPos;

        private Rigidbody2D rigidbody2D;
        private int hotindex = 0;
        public override int HotIndex => hotindex;
        private float moveSpeed = 0;
        public EnemyFathers dics;
        public float maxFlyTime;
        public override BoxCollider2D boxCollider => SelfBoxCollider2D;

        public override float MaxBulletFlyTime => maxFlyTime;

        public override void BulletMoveDic(Vector2 dic)
        {
            base.BulletMoveDic(dic);
        }

        public void WithEnemys(EnemyFathers dic)
        {
            dics = dic;
        }

        public override void Dis()
        {
            base.Dis();

            //Debug.Log("飞刀被销毁");
            Destroy(this.gameObject);
           
        }
        public override void SetBulletScale(float scale)
        {
            base.SetBulletScale(scale);
            this.transform.localScale = new Vector3(scale, scale, scale);
        }

        public override void SetBulletSpeed(float speed)
        {
            base.SetBulletSpeed(speed);
            moveSpeed = speed;
        }
        private void Awake()
        {
            rigidbody2D = this.GetComponent<Rigidbody2D>();
        }

        void Start()
		{
            SelfBoxCollider2D.OnTriggerEnter2DEvent(collider =>
            {
                Debug.Log(collider.gameObject.name);

                var hitbox = collider.GetComponent<HitHurtBox>();

                if (hitbox != null)
                {
                    if (hitbox.Owner.CompareTag("Enemy"))
                    {
                        var enemys = hitbox.Owner.GetComponent<EnemyFathers>();
                        if (enemys != null && enemys is IEnemy enemy)
                        {
                            hotindex += 1;
                            // Debug.Log("接触到敌人");
                            //DamageSystem.SetDamage(DataManager.Instance.simpleKnifeModel.AbilityDamage.Value, enemys);
                            //Dis();
                            if (hotindex >= DataManager.Instance.abilityUpModel.CanPenetrationIndex.Value)
                            {
                               // Debug.Log("到达穿刺上限");
                                DamageSystem.SetDamage(DataManager.Instance.simpleKnifeModel.AbilityDamage.Value, enemys);
                                Dis();
                            }
                            else
                            {
                                //Debug.Log("可以额外穿刺");
                                DamageSystem.SetDamage(DataManager.Instance.simpleKnifeModel.AbilityDamage.Value, enemys);
                            }



                        }
                    }
                }
               
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

        private void Update()
        {
            BulletMove();
        }

        void BulletMove()
        {

            if ((Player.Instance.transform.position-this.transform.position).magnitude>=15)
            {
                Destroy(this.gameObject);
            }
            else
            {

                if (dics==null)
                {
                    Destroy(this.gameObject);
                }
                else
                {
                    var dictory = dics.NormalizedDirection2DFrom(Player.Instance);

                    this.transform.up = dictory;

                    //var direction = this.NormalizedDirection2DFrom(Player.Instance);

                    rigidbody2D.velocity = (dictory * DataManager.Instance.simpleKnifeModel.AbilityItemSpeed.Value);
                }

              
            }


        }
    }
}
